package com.ityunhe.sevlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ityunhe.bean.Account;
import com.ityunhe.bean.Garden;
import com.ityunhe.biz.GardenAccountBis;
import com.ityunhe.biz.UserAccountBis;

@WebServlet("/home/UserAccountServlet")
public class UserAccountServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("这里是UserAccountServlet");
		UserAccountBis userAccountBis = new UserAccountBis();
		//取得数据
		String opr = request.getParameter("opr");
		//如果是注册
		if("regist".equals(opr)){
			//取得数据
		String username=request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String usermobile = request.getParameter("usermobile");
		//创建对象
		Account account = new Account();
		//为对象辅助
		account.setUsername(username);
		account.setUserpassword(userpwd);
		account.setMobile(usermobile);
		int result=0;
		try {
			result = userAccountBis.registAccount(account);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//注册成功
		if(result>0){
			//跳转到登录页并传递账号密码
			request.setAttribute("account", account);
			//id=1代表是user
			request.getRequestDispatcher("/home/login.jsp?id=1").forward(request, response);	
		}else{
			request.setAttribute("error", "注册失败");
			request.getRequestDispatcher("/home/register.jsp").forward(request, response);	
		}
	
		}
		
		
		
		
		
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

}
